package cn.com.czpb.client.module.news.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.MyActivitymanager;
import cn.com.czpb.client.app.ToolBarActivityWhite;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.news.HighRiseBean;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.model.news.NewsListEntity;
import cn.com.czpb.client.module.news.adapter.NewsItemListViewAdapterNew;
import cn.com.czpb.client.module.news.view.TopViewHighrise;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import cn.com.czpb.client.utils.StatUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.litepal.crud.DataSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/10/21.
 */

public class HighriseDetialActivity extends ToolBarActivityWhite implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private int page = 1;
    private static final int pageSize = 15;//
    // 是否刷新最新数据
    private boolean mFlagRefresh;
    @BindView(R.id.recycler_id)
    RecyclerView recyclerId;
    private NewsItemListViewAdapterNew adapter;

    private TopViewHighrise topViewHighrise;
    private HighRiseBean bean;

    @Override
    public int setMyContentView() {
        return R.layout.activity_highrisedetail;
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        bean = (HighRiseBean) intent.getSerializableExtra("bean");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerId.setLayoutManager(manager);
        adapter = new NewsItemListViewAdapterNew(activity,null, false);
        recyclerId.setAdapter(adapter);
        refresh.setRefreshHeader(new ClassHeader_other(activity));
        topViewHighrise = TopViewHighrise.instance(this);
        topViewHighrise.setData(bean);
        adapter.addHeaderView(topViewHighrise.getView());
        adapter.setOnItemChildClickListener(this);
        adapter.setOnLoadMoreListener(this,recyclerId);
    }
    @Override
    public void initData() {
        getServerList();
    }
    /**
     * 获取新闻数据
     */
    public void getServerList() {
        LogUtils.i("getserverlist-->"+bean.getCid());

        Map<String, String> map = new HashMap<>();
        map.put("cid", bean.getCid());
        map.put("Page", page + "");
        map.put("PageSize", pageSize + "");
        Factory.provideHttpService()
                .highrisenewslist(ParamUtils.getRequestParam(map))
                .subscribeOn(Schedulers.io())
                .filter(new Func1<NewsListEntity, Boolean>() {
                    @Override
                    public Boolean call(NewsListEntity entity) {
                        if ("200".equals(entity.code)) {
                            LogUtils.i("200--list>成功");
                            return true;
                        }
                        LogUtils.i("200--list>失败");
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NewsListEntity>() {
                    @Override
                    public void call(NewsListEntity entity) {
                        disMissDialog();
                        if (mFlagRefresh) {
                            refresh.finishRefresh();
                        }

                        if ("200".equals(entity.code)) {
                            setListData(entity.data);
                            if (mFlagRefresh) {
                                LogUtils.i("200-->saveAll");
                                DataSupport.deleteAll(NewsBean.class);
                                DataSupport.saveAll(entity.data);
                            }

                        } else {
//                            TUtils.toast(entity.msg);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (mFlagRefresh) {
                            refresh.finishRefresh();
                            List<NewsBean> list = DataSupport.findAll(NewsBean.class);
                            setListData(list);
                        }
                        LogUtils.i("异常-->" + throwable.toString());
                        adapter.loadMoreFail();
                    }
                });


    }

    public void setListData(List<NewsBean> list) {
        if (mFlagRefresh) {
            adapter.setNewData(list);
            adapter.notifyDataSetChanged();
        } else {
            adapter.addData(list);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->" + adapter.getData().size());
        if (list.size() < pageSize) {
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        final NewsBean newsBean = (NewsBean) adapter.getItem(position);
        switch (view.getId()) {
            case R.id.newsItem_root:
                ////REFACTOR: 2017/8/18 添加浏览量bug
                StatUtils.addCount(newsBean);
                MyActivitymanager.pushActivity(activity, newsBean, newsBean.getTid(), "newsitem", newsBean.getRvalue());
                LogUtils.e("rtype--" + newsBean.getRtype());
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        mFlagRefresh = false;
        page++;
        getServerList();
    }
}
